define(function(require){
    var main =require('/static/main.js');
    require('/static/ajaxfileupload.js');
    var m = new main();
    function Cont(){
        this.Fun._init();
    }
    Cont.prototype.Fun = {
        ENTCode : $_GET['entcode'],
        EntMytipId : 0,
        _init:function(){
            this.getlist();
            this.getlevellist();
            this.bindEvent();
            this.changeHead();
        },
        bindEvent:function(){
            var _this = this;
            // 点击修改信息 + 添加按钮
            $('.add').click(function(){
                var $this = $(this);
                var CustomerId = parseInt($(this).attr('data-id'));
                var UserPhoto = $('#Photo').attr('data-src');
                var data = {
                    Nickname:$('.Nickname').val().trim(),
                    Phone:$('.Phone').val().trim(),
                    IDCard:$(".IDCard").val().trim(),
                    Sex:parseInt($('#Sex option:selected').val()),
                    Photo: UserPhoto ? UserPhoto.trim() : '',
                    MytipLevel:$('#MytipLevel option:selected').val().trim(),
                    ENTCode:_this.ENTCode
                }
                if(data.Photo.length < 1){
                    layer.msg('请上传用户头像');
                    return false;
                }
                if(data.Phone.length < 1){
                    layer.msg('请输入手机号');
                    return false;
                }
                if(!m.IsPhone(data.Phone)){
                    layer.msg('请输入正确的手机号');
                    return false;
                }
                if(data.IDCard.length < 1){
                    layer.msg('请输入身份证号');
                    return false;
                }
                if(!m.isCardNo(data.IDCard)){
                    layer.msg('请输入正确的身份证号');
                    return false;
                }
                if(data.MytipLevel.length < 1){
                    layer.msg('请选择员工等级');
                    return false;
                }
                if(_this.EntMytipId > 0){
                    data.EntMytipId = _this.EntMytipId;
                }
                $.ajax({
                    type: "POST",
                    url: "/enterpriseplatform/enterprise-add-employee",
                    data:data,
                    dataType: "json",
                    success: function (ret) {
                        if(ret.IsBizSuccess){
                            if(_this.EntMytipId){
                                layer.msg('修改成功');
                            }else{
                                layer.msg('添加成功');
                            }
                            $('#AddEmployee').modal('hide');
                            _this.reset(100);
                            var table = $('#datatable').DataTable();
                            table.ajax.reload();
                        }else{
                            layer.msg(ret.ErrMsg);
                            return false;
                        }
                    }
                });
            })

            $('#AddEmployee').on('hide.bs.modal', function () {
                _this.reset(100);
            });
        },
        getlevellist:function(){
            var _this  = this;
            $.ajax({
                type: "POST",
                url: "/enterpriseplatform/get-employee-level-list",
                data:{ENTCode:_this.ENTCode},
                dataType: "json",
                success: function (ret) {
                    var str = '<option value="">选择员工等级</option>';
                    $.each(ret.Models,function(k,v){
                        str +='<option value="'+ v.MytipLevelId+'">'+ v.LevelName+'</option>';
                    });
                    $("#MytipLevel").html(str);
                }
            });
        },
        getlist:function(){
            var _this = this;
            var emdataa = [];
            var param = {};
            var sort = 0;
            var table = $('#datatable').dataTable({
                'ordering':false,
                "serverSide": true,
                "searching":false,
                'bLengthChange': false,
                'bPaginate': true,
                "bDestory": true,
                "bRetrieve": true,
                "bFilter":false,
                "bSort": false,
                "bProcessing": true,
                "autoWidth": false,
                ajax: function (data, callback, settings) {
                    //封装请求参数
                    param.PageSize = data.length;//页面显示记录条数，在页面显示每页显示多少项的时候
                    param.start = data.start;//开始的记录序号
                    param.PageNo = (data.start / data.length)+1;//当前页码
                    param.ENTCode = _this.ENTCode;//当前页码


                    sort = (param.PageNo-1)*param.PageSize;
                    //ajax请求数据
                    $.ajax({
                        type: "POST",
                        url: "/enterpriseplatform/get-enterprise-mytip-list",
                        cache: false,  //禁用缓存
                        data: param,  //传入组装的参数
                        dataType: "json",
                        success: function (result) {
                            var returnData = {};
                            returnData.draw = data.draw;
                            returnData.recordsTotal = result.total;
                            returnData.recordsFiltered = result.total;
                            returnData.data = result.data;
                            emdataa = result.data;
                            callback(returnData);
                        }
                    });
                },
                "aoColumns":[
                    {
                        "mDataProp": 'Photo',
                        "fnCreatedCell": function (nTd, sData, oData, iRow, iCol) {
                            $(nTd).html('<img style="with:60px;height:40px;" src="'+oData.ImgHost +oData.Photo +'">');
                        }
                    },
                    {"mDataProp": 'Nickname'},
                    {"mDataProp": 'Phone'},
                    {"mDataProp": 'Sex',
                        render:function(data){
                            var Sex = "";
                            switch (data){
                                case 1 : Sex = '男';break;
                                case 0 : Sex = '女';break;
                                case -1 : Sex = '保密';break;
                            }
                            return Sex;
                        }},
                    {"mDataProp": 'SourceName'},
                    {"mDataProp": 'IsSuper',
                        render:function(data){
                           return  data ? '是' : '否';
                        }
                    },
                    {"mDataProp": 'CreateTime',
                        render:function(data){
                            return m.formatTimeAll2(data);
                        }
                    },
                    {
                        "mDataProp": "MytipId",
                        "sClass": "option_btn",
                        "fnCreatedCell": function (nTd, sData, oData, iRow, iCol) {
                            // console.log(arguments);IsSuper
                            var str = '<span class="btn_detial EditBtn" data-id="'+sData+'">编辑</span>';
                            if(!oData.IsSuper){
                                str += '<span class="btn_del DelBtn"  data-id="'+sData+'">删除</span>\
                                            <span class="btn_detial SetSupper" data-id="'+sData+'">设为超级管理员</span>';
                            }
                            $(nTd).html(str);

                        }
                    }
                ]
            }).api();

            // 点击搜索
            $('#btnSearch').click(function(){
                param.NickName = $('.SearchNickName').val();
                param.Phone = $('.SearchPhone').val();
                var table = $('#datatable').DataTable();
                table.ajax.reload();

            });
            // 点击重置按钮
            $('#btnRefresh').click(function(){
                $('.SearchNickName').val('');
                $('.SearchPhone').val('');
                param.NickName = '';
                param.Phone = '';
                var table = $('#datatable').DataTable();
                table.ajax.reload();
            });

            //编辑
            $(document).on('click','.EditBtn',function(){
                var MytipId = parseInt($(this).attr("data-id"));
                $.each(emdataa,function(k,v){
                    if(v.MytipId == MytipId){
                        $("#Photo").attr("src", v.ImgHost +''+ v.Photo);
                        $("#Photo").attr("data-src",v.Photo);
                        $("#AddEmployee .Nickname").val(v.Nickname);
                        $("#Sex").val(v.Sex);
                        $("#AddEmployee .Phone").val(v.Phone);
                        $("#AddEmployee .IDCard").val(v.IDCard);
                        _this.EntMytipId = v.MytipId;
                        $("#AddEmployee .modal-title").html('编辑员工信息');
                    }
                });
                $("#AddEmployee").modal('show');
            });
            // 删除
            var isactive = false;
            $(document).on('click','.DelBtn',function(){
                if(isactive == true){
                    return false;
                }
                var id = $(this).attr('data-id');
                isactive = true;
                $.ajax({
                    type: "POST",
                    url: "/enterpriseplatform/enterprise-delete-employee",
                    data :{EntMytipId:id},
                    dataType: "json",
                    success: function (ret){
                        isactive = false;
                        if(ret.IsBizSuccess){
                            layer.msg("删除成功");
                            var table1 = $('#datatable').DataTable();
                            table1.ajax.reload();
                        }else{
                            layer.msg(ret.ErrMsg);
                        }
                    }
                });
            });


            //设为超级管理员
            var issupper = false;
            $(document).on('click','.SetSupper',function(){
                if(isactive == true){
                    return false;
                }
                var id = $(this).attr('data-id');
                layer.confirm('确定转移超级账户至当前所选账号吗？', {
                    btn: ['确定','取消'] //按钮
                }, function(){
                    issupper = true;
                    $.ajax({
                        type: "POST",
                        url: "/enterpriseplatform/enterprise-set-super",
                        data :{EntMytipId:id},
                        dataType: "json",
                        success: function (ret){
                            issupper = false;
                            if(ret.IsBizSuccess){
                                layer.msg("成功更改超级管理员");
                                var table1 = $('#datatable').DataTable();
                                table1.ajax.reload();
                            }else{
                                layer.msg(ret.ErrMsg);
                            }
                        }
                    });
                });

            });
        },
        reset:function(type){
            if(type == 100){
                $("#Photo").attr("src",'');
                $("#Photo").attr("data-src",'');
                $("#AddEmployee .Nickname").val('');
                $("#Sex").val('-1');
                $("#AddEmployee .Phone").val('');
                $("#AddEmployee .IDCard").val('');
                this.EntMytipId = 0;
                $("#AddEmployee .modal-title").html('添加企业管理员');
            }else{

            }
        },
        changeHead:function(){
            var _this = this;
            //图片上传
            $("#file").on("change",function () {
                ajaxFileUpload();
            });
            function ajaxFileUpload(){
                console.log(1);
                $.ajaxFileUpload({
                    url:m.baseUrl+"/customer/edit-head",
                    secureuri: false,
                    fileElementId: 'file',
                    dataType: 'json',
                    success: function (ret) {
                        if(ret.IsBizSuccess){
                            $(".UserPhoto").attr({"src":ret.Photo,'data-src':ret.FileName})
                        }else{
                            layer.msg(ret.ErrMsg);
                            return false;
                        }
                    }
                })
                return false;
            }
        },
    }
    var a = new Cont();

})